package com.example.tests;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Random;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import com.example.fw.ApplicationManager;

import static com.example.tests.GroupDataGenerator.generateRandomGroups;
import static com.example.tests.ContactDataGenerator.generateRandomContacts;

public class TestBase {
	
	protected static ApplicationManager app;
	protected static List<String> birthDayList;
	protected static List<String> birthMonthList;
	protected static List<String> groupsList;
	private int checkFrequency;
	private int checkCounter;

	@BeforeTest
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configFile)));
		app = new ApplicationManager(properties);
		//fillContactRefLists();
		checkCounter = 0;
		checkFrequency = Integer.parseInt(properties.getProperty("check.frequency", "0"));
	  }
	
	protected boolean wantToCheck() {
		checkCounter++;
		if (checkCounter >checkFrequency) {
			return true;
		} else {
			return false;
		}
	}
	
	public void fillContactRefLists() throws Exception {
		app.navigateTo().mainPage();
		app.getContactHelper().goToAddContactPage();
		groupsList = app.getContactHelper().getGroupsListFromAddContactPage();
		app.navigateTo().mainPage();
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		app.stop();  
	  }
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator() {
		return wrapGroupsForDataProvider(generateRandomGroups(5)).iterator();
	}

	public static List<Object[]> wrapGroupsForDataProvider(List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups) {
			list.add(new Object[]{group});
		}
		return list;
	}

	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator() throws IOException {
		return wrapContactsForDataProvider(generateRandomContacts(5)).iterator();
	}
	
	public static List<Object[]> wrapContactsForDataProvider(List<ContactData> contacts) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactData contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list;
	}

	public static String generateRandomString(){
		Random rnd = new Random();
		String rndString = new String();
		if (rnd.nextInt(3)==0){
			return  "";
		} else{
		char[] text = new char[4];
		for (int i = 0; i < 4; i++){
			text[i] = (char)(97+ rnd.nextInt(25));
 			rndString = rndString + text[i];
		}
		return rndString + rnd.nextInt();
		}
	}
	
	private static Date getRandomDate()  {
		Random rnd = new Random();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
		int currentDateAsInt = (int)(System.currentTimeMillis()/100000);
		
		// random date limited by current date: 
		int lowerDateLimit;
		try {
			lowerDateLimit = (int)(sdf.parse("1900.01.01").getTime()/100000);
			long l = rnd.nextInt(currentDateAsInt - lowerDateLimit) + lowerDateLimit;
			l *= 100000;
			date.setTime(l);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
		return date;
    }
	
	private static String[] dateToStringArray(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("d MMMM yyyy", Locale.US);
		String dateString = sdf.format(date);
		String[] resDate = dateString.split(" ");
		return resDate;
	}
	
	public static String[] generateRandomBirthDate() {
		String[] stringArrayDate = new String[3];
		stringArrayDate = dateToStringArray(getRandomDate());
		Random rnd = new Random();
		if (rnd.nextInt(4)==0) {
			stringArrayDate[0] = "-";
		}
		if (rnd.nextInt(4)==0) {
			stringArrayDate[1] = "-";
		}
		if (rnd.nextInt(4)==0) {
			stringArrayDate[2] = "";
		}
		return stringArrayDate;
	}

	public int getRandomIndex(int listSize) {
		Random rnd = new Random();
	    int index = rnd.nextInt(listSize-1);
	    return index;
	}
}
